﻿/// <summary>
/// Developer: CHOOI KAH WAI
/// Last Modified: 23/10/2009
/// </summary>
/// 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Effects;
using ARSS.Client;

namespace ARSS.Visual
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        #region Functions

        /// <summary>
        /// Constructor
        /// </summary>
        public MainWindow()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(MainWindow_Loaded);
            Closed += new EventHandler(MainWindow_Closed);
        }

        /// <summary>
        /// Window closed event handler
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void MainWindow_Closed(object sender, EventArgs e)
        {
            Core.Instance.OnWindowClosed(this);
        }

        /// <summary>
        /// Window loaded event handler
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            Core objCore = Core.Instance;

            string[] sz = objCore.GetCamDevicesName();

            foreach (string s in sz)
                CameraListComboBox.Items.Add(s);

            if (sz.Count() > 0)
                CameraListComboBox.SelectedIndex = 0;
            else
                ConnectButton.IsEnabled = false;

            NicknameTextBox.Focus();
        }

        /// <summary>
        /// Connect button click event handler
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ConnectButton_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            Core.Instance.SelectCamDevice(CameraListComboBox.SelectedIndex);

            Core.Instance.MyData.Name = NicknameTextBox.Text;

            ConnectingDialog cd = new ConnectingDialog();
            cd.Owner = this;
            cd.ServerIPAddress = HostIPTextBox.Text;
            cd.ServerPort = int.Parse(HostPortTextBox.Text);
            this.Effect = new System.Windows.Media.Effects.BlurEffect();


            // Show connection waiting dialog
            if (cd.ShowDialog().Value)
            {
                // Successfully connected.
                Core.Instance.CurrentState = Core.emSTATE.emMAIN;
            }
            else
            {
                // Connection failed.
                this.Effect = null;
            }
        }

        #endregion
    }
}
